package com.wzb.mybatisplus.bitaction.electrumaction.btc01;


import com.wzb.mybatisplus.bitaction.BitcoinPrivateKeyGenerator1;
import com.wzb.mybatisplus.bitaction.electrumaction.FileUtils;
import com.wzb.mybatisplus.bitaction.electrumaction.one.BitcoinKeyConverter;
import com.wzb.mybatisplus.bitaction.electrumaction.one.BlockchainBalanceChecker;
import com.wzb.mybatisplus.bitaction.electrumaction.one.KeyPair;
import com.wzb.mybatisplus.bitaction.electrumaction.three.SendEmailToQQ;
import com.wzb.mybatisplus.entity.Bituse;
import org.json.JSONObject;

import java.math.BigInteger;
import java.security.SecureRandom;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class BaoliTest {

    public static final BigInteger CURVE_ORDER = new BigInteger(
            "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141", 16
    );
    private static final BigInteger MIN_PRIVATE_KEY = BigInteger.ONE;

//    public static void main(String[] args) {
//        // 私钥转公钥
//        String wifPrivateKey = "L5G7x2N61aM14JjUGD7LR5KQwSdg6N68FhmJxm54i36BRUX2ohCa";
////        BigInteger privateKey = "";
//        KeyPair wifKeyPair = null;
//        try {
//            wifKeyPair = BitcoinKeyConverter.generateFromWIF(wifPrivateKey);
//        } catch (Exception e) {
//
//        }
//        String address = wifKeyPair.getLegacyAddress(); // 比特币创始区块地址公钥地址
//        System.out.println(address);
//    }
    public static void main(String[] args) {

        BigInteger huge1 = new BigInteger("CURVE_ORDER");
        BigInteger huge2 = new BigInteger("2");

        BigInteger result = huge1.divide(huge2);
        System.out.println("结果: " + result);
//        BaoliTest baoliTest = new BaoliTest();
//        baoliTest.baoli("115792089237316195423570985008687907852837564279074904382605163141518159467836","-1");
    }


    public void baoli(String ad, String add ){
        int conutsum =0;
        String path = "D:\\btc\\miyao\\maney.txt";
        FileUtils fileUtils = new FileUtils();
        fileUtils.createFile(path);
        int j = 0;
        Set<String> addresses =  new HashSet<>();
        Set<String> addressesSys = new HashSet<>();
        BigInteger a = new BigInteger(ad);
        BigInteger sy = a;
        for (int i = 0; conutsum < 5000; i++){
            //随机生成私钥

            if(i>10000000){i=0;j++;}
//                BigInteger b = new BigInteger((i*10000)+"");

            BitcoinPrivateKeyGenerator1.BitcoinPrivateKey privateKey = generatePrivateKey(sy,new BigInteger(add));

            if (BitcoinPrivateKeyGenerator1.isValidPrivateKey(privateKey.getPrivateKey())) {  //私钥正确的话

                // 私钥转公钥
                String wifPrivateKey = privateKey.getWIFCompressed();

                KeyPair wifKeyPair = null;
                try {
                    wifKeyPair = BitcoinKeyConverter.generateFromWIF(wifPrivateKey);
                } catch (Exception e) {

                }
                if(wifKeyPair==null){
                    sy = new BigInteger(privateKey.getHex(),16).add(new BigInteger(add));
                    continue;
                }
                String address = wifKeyPair.getLegacyAddress(); // 比特币创始区块地址公钥地址
                addresses.add(address);
                addressesSys.add(wifPrivateKey);
            }
//                System.out.println(privateKey);
            sy = new BigInteger(privateKey.getHex(),16).add(new BigInteger(add));
            if(addresses.size()>=300){


                List<String> list = new ArrayList<>();
                list.addAll(addresses);
                List<String> listSys = new ArrayList<>();
                listSys.addAll(addressesSys);
                // 批量调用比特币
                String result = BlockchainBalanceChecker.getBalanceForAddresses(list);
                for(;result==null;){
//                    Thread.sleep(1000);
//                    flag++;
//                    System.out.println("=======调用比特币地址返回空，走进死循环中了======="+flag);
                    result = BlockchainBalanceChecker.getBalanceForAddresses(list);
                }
                JSONObject jsonResponse = new JSONObject(result);
                int z = 0;
                for(String address : addresses){
                    JSONObject addressInfo = jsonResponse.getJSONObject(address);
//                    System.out.println(address+":    "+addressInfo);
                    Long maney = addressInfo.getLong("final_balance");
                    long total_received = addressInfo.getLong("total_received");
                    long jiaoyiliang = addressInfo.getLong("n_tx");
                    if (maney > 0 || jiaoyiliang > 0 || total_received > 0) {
                        System.out.println("=======金额======" + maney + "私钥：" + addressesSys);
                        conutsum++;
                        Bituse bituse = new Bituse();
                        bituse.setWifsy(listSys.get(z));
                        bituse.setMask(maney + "");
                        System.out.println("=======金额======" + maney + "私钥：" + listSys.get(z));
//                            bituseMapper.insert(bituse);
                        if(maney>0){
                            SendEmailToQQ sendEmailToQQ = new SendEmailToQQ();
                            sendEmailToQQ.sendEmail("2267457055@qq.com",addressesSys.toString());
                            SendEmailToQQ sendEmailToQQ1 = new SendEmailToQQ();
                            sendEmailToQQ1.sendEmail("2267457055@qq.com","有效值："+listSys.get(z));
                            String send = "";
                            for(String s : addressesSys){
                                fileUtils.writeFile( s+"\n",path);
                                send=send+"有效值："+s;
                            }
                            SendEmailToQQ sendEmailToQQ3 = new SendEmailToQQ();
                            sendEmailToQQ3.sendEmail("2267457055@qq.com",send);
                            System.out.println();
                        }
                        fileUtils.writeFile( "有效值："+addressesSys+"\n",path);
                        fileUtils.writeFile( "有效值："+listSys.get(z)+"\n",path);
                    }
                    z++;
                }
                addressesSys.clear();
                addresses.clear();
            }


        }


    }
    public static BitcoinPrivateKeyGenerator1.BitcoinPrivateKey generatePrivateKey() {
        try {
            SecureRandom secureRandom = SecureRandom.getInstanceStrong(); // 使用强随机数
            BigInteger privateKeyNum;

            // 确保私钥在有效范围内
            do {
                byte[] privateKeyBytes = new byte[32];
                secureRandom.nextBytes(privateKeyBytes);
                privateKeyNum = new BigInteger(1, privateKeyBytes);
            } while (!isValidPrivateKey(privateKeyNum));
            return new BitcoinPrivateKeyGenerator1.BitcoinPrivateKey(privateKeyNum);

        } catch (Exception e) {
            throw new RuntimeException("生成私钥失败", e);
        }
    }

    public static BitcoinPrivateKeyGenerator1.BitcoinPrivateKey generatePrivateKey(BigInteger privateKeyNum,BigInteger num) {
        try {
//            SecureRandom secureRandom = SecureRandom.getInstanceStrong(); // 使用强随机数

            for(;;){
                if(isValidPrivateKey(privateKeyNum)){break;}
                privateKeyNum = privateKeyNum.add(num);
                if(privateKeyNum.compareTo(MIN_PRIVATE_KEY) < 0){
                    System.out.println("超过数据了跳出循环");
                    System.exit(1);
                }
            }

            return new BitcoinPrivateKeyGenerator1.BitcoinPrivateKey(privateKeyNum);

        } catch (Exception e) {
            throw new RuntimeException("生成私钥失败", e);
        }
    }

    /**
     * 检查私钥是否在有效范围内
     */
    public static boolean isValidPrivateKey(BigInteger privateKey) {
        return privateKey.compareTo(MIN_PRIVATE_KEY) >= 0 &&
                privateKey.compareTo(CURVE_ORDER) < 0;
    }
}
